<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>选择投票人</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="Cache-Control" content="no-store, must-revalidate">
    <meta http-equiv="expires" content="Wed, 26 Feb 1997 08:21:57 GMT">
    <meta http-equiv="expires" content="-1">
    <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta http-equiv="refresh" content="1200">
    <link rel="shortcut icon" href="/dist/img/icon.png" type="image/x-icon">
    <!-- Bootstrap 3.3.6 -->
    <link rel="stylesheet" href="/dist/plugins/bootstrap/css/bootstrap.min.css">
    <!--字体-->
    <link rel="stylesheet" href="/dist/css/font-awesome.min.css">
    <!--图标-->
    <link rel="stylesheet" href="/dist/css/ionicons.min.css">
    <!--插件样式-->
    <link rel="stylesheet" href="/dist/plugins/fixedTable/fixed-table.css"/>
    <!--OA主样式-->
    <link rel="stylesheet" href="/dist/css/OA.min.css?t=39cd89cd">
    <link rel="stylesheet" href="/dist/css/skins/OA-skins.min.css?t=16f1e6f3">
    <link href="/dist/plugins/ztree/css/zTreeStyle/zTreeStyle.css" rel="stylesheet"/>
    <link href="/page/voteApp/votePublic.css" rel="stylesheet"/>
    <style>

    </style>
</head>
<body>

<section class="content" id="app" v-cloak>
    <div class="treeBox" v-show="userModel">
        <div class="tabContent">
            <div class="tabContentLeft"></div>
            <div class="tabContentMiddle">

             <span  style="cursor: pointer" :class="selectTree==0? 'tabActive':'tabNo'" @click="selectTree=0;changeTree()">
                    按组织结构选择
                </span>
                <span style="cursor: pointer" id="cusBtn" :class="selectTree==1? 'tabActive':'tabNo'" @click="selectTree=1;">
                    按部门岗位选择
                </span>

            </div>
            <div class="tabContentRight"></div>
        </div>
        <div style="overflow: auto;background: #f7f8fa;margin: 15px 20px;margin-top:60px;border: 1px solid #ddd;">
            <div class="selectBox" v-show="selectTree==1">
                <div class="typeTitle">请选择公司：</div>
                <div class="companyLIst listBox">
                    <div :class="item.select?'item active':'item'" v-for="(item,i) in selectBox.companyList" @click="selectItem(item,'1')">{{item.name}}</div>
                </div>
                <div class="typeTitle">请选择部门：</div>

                <div class="companyLIst listBox">
                    <div :class="item.select?'item active':'item'" v-for="(item,i) in selectBox.departList"  @click="selectItem(item,'2')">{{item.departName}}</div>
                    <div class="noData" v-if="selectBox.departList.length==0">请先选择公司</div>
                </div>
                <div class="typeTitle">请选择职位：</div>
                <div class="companyLIst listBox">
                    <div :class="item.select?'item active':'item'" v-for="(item,i) in selectBox.roleList"  @click="selectItem(item,'3')">{{item.roleName}}</div>
                    <div class="noData" v-if="selectBox.roleList.length==0">请先选择部门</div>
                </div>
            </div>
            <div v-show="selectTree==0">
                <div class="inputTreeBox">
                    <input type="text" class="selecetInput" placeholder="输入姓名" id="autoUser" v-on:input="get_search()">
                    <input type="button" class="search-button">
                </div>

                <ul class="ztree" id="ulRegion" style="height: 75vh;overflow-y: auto;" ></ul>
            </div>
        </div>
        <input type="button" value="确认" id="btnSave" class="btn oaBtn" @click="save"
               style="margin-bottom:10px;">
    </div>
</section>


<!--JQ-->
<script src="https://file.ggxqce.com/web/jquery-2.2.3.min.js"></script>
<!--插件JS-->
<script src="https://file.ggxqce.com/web/bootstrap.min.js"></script>
<!--框架已定义 JS-->
<script src="/dist/js/app_iframe.js?t=7e54a132"></script>
<!--oa自定义  JS-->
<script src="/dist/js/oa.js?t=558d0414"></script>
<script type="text/javascript" src="/dist/plugins/My97DatePicker/WdatePicker.js"></script>
<script type="text/javascript" src="/dist/plugins/ztree/js/jquery.ztree.all-3.5.min.js"></script>
<script type="text/javascript" src="/dist/plugins/ztree/js/jquery.ztree.exhide-3.5.js"></script>
<script src="https://file.ggxqce.com/web/vue.min.js"></script>
<script>
    $(function () {
        $("body").on("click", ".lineOne,.lineTwo", function (e) {
            if ($(".div-td-content-more").is(":hidden")) {
                var text = $(this).text();
                if (text != "") {
                    text = "<i></i>" + text;
                    $(".div-td-content-more").html(text).css({
                        "width": $(this).outerWidth() + "px",
                        "left": $(this).offset().left,
                        "top": ($(this).offset().top + $(this).outerHeight()) + "px"
                    }).show();
                    e.stopPropagation();
                }
            } else {
                $(".div-td-content-more").hide();
            }
        });
        $("body").on("click", ".div-td-content-more", function (e) {
            e.stopPropagation();
        });
        $("body").on("click", function () {
            $(".div-td-content-more").hide();
        });
    });
    $.ajaxSetup({
        global: true,
        type: "POST",
        cache: false,
        dataType: "json",
        contentType: 'application/json;charset=utf-8',
        beforeSend: function () {
        },
        complete: function () {

        },
        error: function (e) {
            alert('error' + e.responseText);
        }
    });
    $(function () {
        var zTreeRegion_setting = {
            view: {
                showIcon: function (treeId, treeNode) {
                    return treeNode.isParent;
                }
            },
            data: {
                simpleData: {
                    enable: true
                }
            },
            check: {
                chkboxType: { "Y": "ps", "N": "ps" },
                enable: true
            },
            callback: {
                beforeClick:zTreeBeforeClick
            }
        };
        $.ajax({
            url: server.ip + "/system/getCDUAll.json",
            data: JSON.stringify({}),
            success: function (r) {
                var nodes=r.data.sendData;
                for (var i = 0; i < nodes.length; i++) {
                    if (nodes[i].name == "收件箱") {
                        nodes[i].name ="组织机构";
                    }
                }
                app.treeObj = $.fn.zTree.init($("#ulRegion"), zTreeRegion_setting, nodes);

                app.treeObj.getNodesByFilter(function (node) {
                    if(app.voteUsers.indexOf(node.id)>-1) {
                        app.treeObj.checkNode(node, true, true, false);
                    }else{
                        app.treeObj.checkNode(node, false, false, false);
                    }
                });


            }
        });

        function zTreeBeforeClick(treeId, treeNode, clickFlag) {
            app.treeObj.checkNode(treeNode, '', true);
            return false;
        }

    });
</script>
<script type="text/javascript">
    var api = frameElement.api, W = api.opener;
    var query = null;
    var app = new Vue({
        el: '#app',
        data: {
            type:'',
            mainId:$.wyui.page.urlParams.mainId,
            voteUsers:$.wyui.page.urlParams.voteUsers,
            selectTree:0,
            selectBox:{
                companyList:[],
                departList:[],
                roleList:[],
                selectCompany:[],
                companyNames: [],
                departNames: [],
                roleNames: [],
                companyIds: [],
                userIds:[]
            },
            userModel:true  ,
            lookUserModel:true,
            lookUserList:[],
            treeObj:"",
            userId: getCurrentUser().userId,
            userInfo:{},
            date: "",
            mainInfo:{},
            tabList:[],
            info: {
                auditors:[],
                auditFlag:"-2",
                auditInfo:"",
                title: "",
                content: "",
                endTime:'',
                uploadTime:'',
                voteUsers:$.wyui.page.urlParams.voteUsers?$.wyui.page.urlParams.voteUsers:'',
                createUserId:getCurrentUser().userId,
                companyId:"",
                createUserName:"",
                voteThemeList: [
                    {
                        "title": "",
                        "value": [
                            { "title": "", "disa":true,"attrUrl":"","attrName":""},
                            { "title": "", "disa": true,"attrUrl":"","attrName":"" },
                            { "title": "", "disa": true,"attrUrl":"","attrName":"" },
                            { "title": "", "disa": true ,"attrUrl":"","attrName":""}
                        ]
                    }
                ]
            },
            isGo:true,
            isOpenBtn:false
        },
        methods: {
            changeTree:function () {
                var that=this;


                if(that.selectBox.userIds.length>0){
                    var userIdsStr=that.selectBox.userIds.join(',')
                    this.treeObj.getNodesByFilter(function (node) {
                        if(userIdsStr.indexOf(node.id)>-1) {
                            console.log(node.id)
                            that.treeObj.checkNode(node, true, true, false);
                        }else{
                            that.treeObj.checkNode(node, false, false, false);
                        }
                    });
                }

            },
            getCompanyList:function () {
                var that=this;
                $.wyui.postMethod('/company/getCompanyByUserId.json', {userId: '201711271733404C9DA1461F2421F828'}, function (data) {
                    var isgo=false;
                    for (var i = 0; i < data.length; i++) {
                        if (that.selectBox.companyIds.indexOf(data[i].id) != -1) {
                            isgo=true;
                            data[i].select =true
                        } else {
                            data[i].select = false
                        }
                    }
                    that.selectBox.companyList = data
                    if(isgo){
                        that.selectDepart();
                    }
                })
            },
            selectDepart: function () {
                var that = this;
                if (that.selectBox.companyIds.length > 0) {
                    var companyIds = that.selectBox.companyIds.join(',');
                    $.wyui.postMethod('/exam/getDepartListByCompanyIds.json', {companyIds: companyIds}, function (data) {
                        var isgo = false;
                        var newList = [];
                        console.log(data)
                        for (var i = 0; i < data.result.length; i++) {
                            if (that.selectBox.departNames.indexOf(data.result[i].departName) != -1) {
                                data.result[i].select = true;
                                newList.push(data.result[i].departName)
                                isgo = true
                            } else {
                                data.result[i].select = false
                            }
                        }
                        that.selectBox.departNames = newList;
                        that.selectBox.departList = data.result;

                        if (isgo) {
                            that.selectRoleName();
                        } else {
                            that.selectBox.roleNames = [];
                            for (var i = 0; i < that.selectBox.roleList.length; i++) {
                                if (that.selectBox.departNames.indexOf(data.result[i].departName) != -1) {
                                    data.result[i].select = true;
                                    newList.push(data.result[i].departName)
                                    isgo = true
                                } else {
                                    data.result[i].select = false
                                }
                            }
                        }
                    })
                }
            },
            selectRoleName: function () {
                var that = this;
                if(that.selectBox.departNames.length>0) {
                    var companyIds=that.selectBox.companyIds.join(',');
                    var departNames=that.selectBox.departNames.join(',');
                    $.wyui.postMethod('/exam/getRoleNameListByCompanyAndDepart.json', {
                        companyIds: companyIds,
                        departNames: departNames
                    }, function (data) {
                        console.log(data);
                        var newList=[];
                        for (var i = 0; i < data.result.length; i++) {
                            if (that.selectBox.roleNames.indexOf(data.result[i].roleName) != -1) {
                                data.result[i].select = true;
                                newList.push(data.result[i].roleName)
                            } else {
                                data.result[i].select = false
                            }
                        }
                        that.selectBox.roleNames=newList;
                        that.selectBox.roleList = data.result
                    })
                }
            },
            /*getAuditors:function () {
                var that=this;
               /!* $.wyui.postMethod('/vote/getAuditList.json', {userId: that.userInfo.id,companyId:that.userInfo.companyId}, function (data) {
                    that.info.auditors= data.auditors;
                })*!/
            },*/
            get_search: function () {
                clearTimeout(query);
                query = setTimeout(function () {
                    searchTree()
                }, 1000);
            },
            getUserIds:function () {
                var that=this;
                $.wyui.postMethod('/exam/getUserList.json', {
                    companyIds:that.selectBox.companyIds.join(','),
                    departNames:that.selectBox.departNames.join(','),
                    pageSize: 500,
                    requestPage: 1,
                    roleNames: that.selectBox.roleNames.join(',')

                }, function (data) {
                    var list=data.records;
                    var userIds=[];
                    if(list.length>0){
                        for(var i=0;i<list.length;i++){
                            userIds.push(list[i].userId)
                        }
                    }
                    that.selectBox.userIds=userIds;
                    console.log(that.selectBox.userIds);
                })
            },
            selectItem: function (item,type) {
                var check;
                if (type == '1') {
                    check = "companyIds"
                    this.checkedOne(check, item.id);
                } else if (type == '2') {
                    check = "departNames"
                    this.checkedOne(check, item.departName);
                } else {
                    check = "roleNames"
                    this.checkedOne(check, item.roleName);
                }
                if (item.select == true) {
                    item.select = false
                } else {
                    item.select =true
                }
                console.log(item)
            },
            checkedOne: function (checkList, moduleKey) {
                var that=this;
                var idIndex = this.selectBox[checkList].indexOf(moduleKey)
                if (idIndex >= 0) {
                    this.selectBox[checkList].splice(idIndex, 1)
                } else {
                    this.selectBox[checkList].push(moduleKey)
                }
                if(checkList=='companyIds'){
                    if(that.selectBox.companyIds.length==0){
                        that.selectBox.departList=[];
                        that.selectBox.departNames=[];
                        that.selectBox.roleList=[];
                    }
                    that.selectDepart()

                }
                if(checkList=='departNames'){
                    if(that.selectBox.departNames.length==0){
                        that.selectBox.roleList=[];
                        that.selectBox.roleNames=[];
                    }

                    that.selectRoleName()
                }
                if(checkList=='roleNames'){
                    if(that.selectBox.roleNames.length>0) {
                        that.getUserIds();
                    }else{
                        that.selectBox.userIds=[];
                    }
                }

            },
            check:function (e,index,index2) {
                var that=this;
                var data=this.info.voteThemeList[index].value[index2].title
                data=$.trim(data)
                if(data){
                    $.wyui.postMethod("/vote/checkName.json", {"name":data}, function (res) {
                        if(!res.flag){
                            console.log(e)
                            $(e).focus();
                            $(e).parent().find(".yz").show();
                            that.info.voteThemeList[index].value[index2].title='';
                        }

                    });

                }

            },
            save: function () {
                var that=this;
                var voteUsers='';
                if(this.selectTree=='1'){
                    $.wyui.postMethod('/exam/getUserList.json', {
                        companyIds:that.selectBox.companyIds.join(','),
                        departNames:that.selectBox.departNames.join(','),
                        pageSize: 500,
                        requestPage: 1,
                        roleNames: that.selectBox.roleNames.join(',')
                    }, function (data) {
                        var list=data.records;
                        var userIds=[];
                        if(list.length>0){
                            for(var i=0;i<list.length;i++){
                                userIds.push(list[i].userId)
                            }
                        }

                        if(userIds.length>0){
                            voteUsers=userIds.join(',')
                        }else{
                            voteUsers='';
                        }

                    },true)
                }else{
                    searchTree(true)
                    var receivers = new Array();
                    var Nodes = this.treeObj.getCheckedNodes(true);
                    for (var i = 0; i < Nodes.length; i++) {
                        if(Nodes[i].type == "user") {
                            receivers.push(Nodes[i].id);
                        }
                    }
                    if(receivers.length>0){
                       voteUsers=receivers.join(',')
                    }else{
                        voteUsers='';
                    }

                }
                var $iframe = top.findIframeByName("投票详情");
                $iframe[0].contentWindow.app.info.voteUsers=voteUsers;
                $iframe[0].contentWindow.app.selectBox.companyIds=that.selectBox.companyIds.join(",");
                $iframe[0].contentWindow.app.selectBox.roleNames=that.selectBox.roleNames.join(",");
                $iframe[0].contentWindow.app.selectBox.departNames=that.selectBox.departNames.join(",");
               /* if(that.mainId!=''){
                    $iframe[0].contentWindow.app.pageType='edit2';
                }*/
                api.close();

            }
        },
        mounted:function (){

            var that=this; //声明一个变量指向Vue实例this，保证作用域一致
            that.getCompanyList();
            if($.wyui.page.urlParams.companyIds){
                that.selectBox.companyIds=$.wyui.page.urlParams.companyIds.split(',')
            }
            if($.wyui.page.urlParams.roleNames){
                that.selectBox.roleNames=$.wyui.page.urlParams.roleNames.split(',')
            }
            if($.wyui.page.urlParams.departNames){
                that.selectBox.departNames=$.wyui.page.urlParams.departNames.split(',')
            }
            console.log(that.selectBox.companyIds)
            console.log(that.selectBox.roleNames)
            console.log(that.selectBox.departNames)

        }

    });

</script>
<script>
    $(".search-button").on('click', function () {
        searchTree()
    });
    function searchTree(type) {
        var text=''
        if(type){
            text = '';
        }else{
            text = $(".selecetInput").val();

        }

        if (text != "") {
            var allNode = app.treeObj.transformToArray(app.treeObj.getNodes());
            ;
            app.treeObj.hideNodes(allNode);
            nodeList = app.treeObj.getNodesByParamFuzzy("name", text);
            nodeList = app.treeObj.transformToArray(nodeList);
            for (var n in nodeList) {
                app.treeObj.selectNode(nodeList[n], true);
                findParent(app.treeObj, nodeList[n]);
            }
            app.treeObj.showNodes(nodeList);

        } else {
            var allNode = app.treeObj.transformToArray(app.treeObj.getNodes());
            ;
            app.treeObj.showNodes(allNode);
            app.treeObj.cancelSelectedNode();
            app.treeObj.expandAll(false);
            for (var index in allNode) {
                if (allNode[index].level < 2) {
                    app.treeObj.expandNode(allNode[index], true, false, false);
                }
            }

        }
        function findParent(treeObj, node) {
            treeObj.expandNode(node, true, false, false);
            var pNode = node.getParentNode();
            if (pNode != null) {
                nodeList.push(pNode);
                findParent(treeObj, pNode);
            }
        }
    }
</script>
</body>
</html>